﻿#include "caddnode.h"
#include "ui_caddnode.h"
#include <QMessageBox>
#include <QColorDialog>
#include <QDebug>

CAddNode::CAddNode(QDialog *parent) :
    QDialog(parent),
    ui(new Ui::CAddNode)
{
    ui->setupUi(this);
    ui->nameLineEidt->clear();
    ui->tooltipLineEdit->clear();
    ui->colorLabel->setStyleSheet("QLabel{background-color: rgb(255, 255, 255);}");
}

CAddNode::~CAddNode()
{
    delete ui;
}

void CAddNode::on_selectBtn_clicked()
{
    m_color = QColorDialog::getColor(Qt::white, this, tr("Select Color"));
    if(m_color.isValid())
    {
        ui->colorLabel->setStyleSheet(QString("QLabel{background-color: rgb(%1, %2, %3);}")
                                      .arg(m_color.red()).arg(m_color.green()).arg(m_color.blue()));
    }
}

void CAddNode::on_okBtn_clicked()
{
    SItemInfo sItemInfo;
    sItemInfo.strName = ui->nameLineEidt->text();
    sItemInfo.strToolTip = ui->tooltipLineEdit->text();
    QString redStr = QString("%1").arg(m_color.red(),2,16,QChar('0'));
    QString greenStr = QString("%1").arg(m_color.green(),2,16,QChar('0'));
    QString blueStr = QString("%1").arg(m_color.blue(),2,16,QChar('0'));
    sItemInfo.strColor = "#"+ redStr + greenStr + blueStr;
//    QRgb mRgb = qRgb(m_color.red(),m_color.green(),m_color.blue());
//    sItemInfo.strColor = QString::number(mRgb, 16);
    if(sItemInfo.strName.isEmpty() || sItemInfo.strToolTip.isEmpty())
    {
        QMessageBox::information(this, "Hint", "Please input all information", QMessageBox::Ok);
        return;
    }
    emit sigAddItemInfo(sItemInfo);
//    qWarning() << "emit sigAddItemInfo";
    accept();
}

void CAddNode::on_cancelBtn_clicked()
{
    reject();
}
